#!/bin/sh
# code envoirment
# conda activate DBNet
export _CMAKELISTS_URL=https://gitee.com/tangzhongliang/public/raw/master/CMakeLists.txt
export _SETUP_URL=https://gitee.com/tangzhongliang/public/raw/master/setup.py
PWD=`pwd`
PYTHON_DIR=`which python`
env_dir=$(dirname $(dirname ${PYTHON_DIR}))
env_name=$(basename $env_dir)
env_tags_file=${HOME}/.vim/tags/${env_name}.tags
project_dir=${PWD}
project_tags_file=${PWD}/.tags
if test -f "$env_tags_file"; then
    echo $env_tags_file' exist'
else
    ctags -R --languages=Python -f $env_tags_file $env_dir
    echo $env_tags_file' create success'
fi
if test -f ".tensorflow.tags"; then
    echo ".tensorflow.tags"
else
    ctags -R --languages=Python -f .tensorflow.tags ~/source_dir/tensorflow-1.13.1
fi
freshcmd='ctags -R -f '$project_tags_file' --languages=Python '$project_dir' & ctags -R --c++-kinds=+px --fields=+iaS --extra=+q -a -f '$project_tags_file' '$project_dir' & find '$project_dir' -name "*.py" > .cscope.files & find '$project_dir' -name "*.yaml" >> .cscope.files & cscope -Rb -i .cscope.files'
ctags -R -f $project_tags_file --languages=Python $project_dir
ctags -R --c++-kinds=+px --fields=+iaS --extra=+q -a -f $project_tags_file $project_dir
find $project_dir \( -name "*.py" -o -name "*.yaml" -o -name "*.h" -o -name "*.hpp" -o -name "*.c" -o -name "*.cpp" \) > .cscope.files
cscope -Rb -i .cscope.files
mv cscope.out .cscope.out

touch .workspace.vim
echo ':nnoremap <F8> <Esc>:w<CR>:!python "%:p"<CR>' > .workspace.vim
echo ':nnoremap <F7> <Esc>:w<CR>:!python setup.py build_ext --inplace <CR>' >> .workspace.vim
echo ':nnoremap <F5> <Esc>:w<CR>:!'$freshcmd' <CR>:UpdateTypesFileOnly <CR>' >> .workspace.vim # F5 recreate tags and update types 
echo 'set tags+='$project_tags_file >> .workspace.vim
echo 'set tags+=.tensorflow.tags' >> .workspace.vim

# 如果系统的UserLibraries没有的话，请自己生成

taghlconfigcmd='IncludeLocals:True\nTagFileName:.tags\nDoNotGenerateTagsIfPresent:True\nUserLibraryDir:'$HOME'/.vim/taghls\nUserLibraries:opencv/types_c.taghl,opencv_contrib/types_c.taghl,c++/types_c.taghl' 
#if test -f 'taghl_config.txt'
#then
#    echo 'taghl_config.txt exist'
#else
#    echo $taghlconfigcmd > taghl_config.txt
#fi
echo $taghlconfigcmd > taghl_config.txt
if test -f ".Session";then
    vim -S .Session -c 'source .workspace.vim | cs add'$project_dir'/.cscope.out' $project_dir
else
    vim -c 'source .workspace.vim | cs add '$project_dir'/.cscope.out' $project_dir
fi
find . -name '*~' | xargs -I {} rm {}
echo 'quit'